package com.hs.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.Coursereply;
import com.hs.sfzy.service.CoursereplyService;
import com.hs.sfzy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName CoursereplyController
 * @Description TODO
 * @Author zgy
 * @Date 2024年10月24日 21:10
 * @Version 1.0
 */
@RestController
@RequestMapping("/coursereply")
public class CoursereplyController {
    @Autowired
    private CoursereplyService coursereplyService;

    /**
     * 添加数据
     *
     * @param coursereply
     * @return com.hs.sfzy.util.Result
     * @author zgy
     * @date 2024/10/24 20:44
     */
    @PostMapping
    public Result insert(@RequestBody Coursereply coursereply) {
        int num = coursereplyService.insert(coursereply);
        if (num != 0) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    /**
     * 根据id删除数据
     *
     * @param id
     * @return com.hs.sfzy.util.Result
     * @author zgy
     * @date 2024/10/24 20:44
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") int id) {
        int num = coursereplyService.delete(id);
        if (num != 0) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    /**
     * 修改数据
     *
     * @param coursereply
     * @return com.hs.sfzy.util.Result
     * @author zgy
     * @date 2024/10/24 20:44
     */
    @PutMapping
    public Result update(@RequestBody Coursereply coursereply) {
        int num = coursereplyService.update(coursereply);
        if (num != 0) {
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }

    /**
     * 查询所有数据
     *
     * @param coursereply
     * @return com.hs.sfzy.util.Result
     * @author zgy
     * @date 20:44
     */
    @GetMapping
    public Result selectAll(@RequestBody Coursereply coursereply) {
        List<Coursereply> list = coursereplyService.selectAll(coursereply);
        return Result.success(list);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return com.hs.sfzy.util.Result
     * @author zgy
     * @date 2024/10/24 20:45
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") int id) {
        Coursereply coursereply = coursereplyService.selectById(id);
        return Result.success(coursereply);
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param coursereply
     * @return com.hs.sfzy.util.Result
     * @author zgy
     * @date 2024/10/24 20:45
     */
    @GetMapping("/findByPage")
    public Result selectByPage(
            @RequestParam(required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "5") int pageSize,
            Coursereply coursereply) {
        PageInfo<Coursereply> info = coursereplyService.selectByPageInfo(pageNum, pageSize, coursereply);
        return Result.success(info);
    }

    @GetMapping("/findByTalkId")
    public Result selectByTalkId(long talkId){
        List<Coursereply> coursereplies = coursereplyService.selectByTalkId(talkId);
        return Result.success(coursereplies);
    }
}
